<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>系统设置</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <script src="./js/global.js"></script>
    <script src="./js/config.js"></script>

</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">系统基础</a>
                <a>
                    <cite>系统设置</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">

                </div>
            </div>
            <div class="layui-card-body ">
                <form class="layui-form" id="configForm">

                </form>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use([ 'form','request'],
        function() {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;
            var request = layui.request;
            var adminToken = localStorage.getItem("adminToken");
            //获得get参数
            var $_GET = (function(){
                var url = window.document.location.href.toString();
                var u = url.split("?");
                if(typeof(u[1]) == "string"){
                    u = u[1].split("&");
                    var get = {};
                    for(var i in u){
                        var j = u[i].split("=");
                        get[j[0]] = j[1];
                    }
                    return get;
                } else {
                    return {};
                }
            })();
            //获取分组title
            request.ajax(requestUri+'/admin/system/config/getconfigtitle', 'get', 'json', {
                'token': adminToken
            }, function (req) {
                if(req.code == 1000){
                    var str = '';
                    $.each(req.data,function(i,item){
                        str +='<button class=\"layui-btn layui-btn-sm\" id=\"'+item+'\" onclick=\"configHref(this.id)\"> '+i+' </button>';
                    });
                    $('.layui-card-header').html(str);
                }
            });
            //获取分组title
            request.ajax(requestUri+'/admin/system/config/getlist', 'get', 'json', {
                'token': adminToken,
                'group':$_GET['group']
            }, function (req) {
                if(req.code == 1000){
                    var str='';
                    $.each(req.data,function(i,item){
                       if(item.type == 'input'){
                           str+=' <div class=\"layui-form-item\">';
                           str+=' <label for=\"'+item.name+'\" class=\"layui-form-label\">';
                           str+='<span class=\"x-red\">*</span>'+item.title+'</label>';
                           str+='<div class=\"layui-input-inline\">';
                           str+=' <input type=\"text\" value=\"'+item.value+'\" id=\"'+item.name+'\" name=\"'+item.name+'\" class=\"layui-input\">';
                           str+='</div>';
                           str+='<div class=\"layui-form-mid layui-word-aux\">';
                           str+='<span class=\"x-red\">*</span>systemConfig(\"'+item.name+'\")获取  ';
                           if(item.tips != ''){
                               str+=item.tips;
                           }
                           str+='</div></div>';
                       }
                       if(item.type == 'select'){
                           str+='<div class=\"layui-form-item\">';
                           str+='<label for=\"type\" class=\"layui-form-label\">';
                           str+='<span class=\"x-red\">*</span>'+item.title+'</label>';
                           str+='<div class=\"layui-input-inline\">';
                           str+=' <select id=\"'+item.name+'\" name=\"'+item.name+'\" class=\"valid\">';
                           var array = foo(item.options);
                           for (var p = 0;p<array.length;p++){
                               var optionStr = array[p];
                               var op = optionStr.split("-");
                               if(item.value == op[0]){
                                   str+='<option value=\"'+op[0]+'\"  selected = \"selected\">'+op[1]+'</option>';
                               }else {
                                   str+='<option value=\"'+op[0]+'\">'+op[1]+'</option>';
                               }
                           }
                           str+=' </select></div>';
                           str+='<div class=\"layui-form-mid layui-word-aux\">';
                           str+='<span class=\"x-red\">*</span>systemConfig(\"'+item.name+'\")获取  ';
                           if(item.tips != ''){
                               str+=item.tips;
                           }
                           str+='</div>';
                       }
                       if(item.type == 'textarea'){
                           str+='<div class=\"layui-form-item layui-form-text\">';
                           str+='<label for=\"options\" class=\"layui-form-label\">'+item.title+'</label>';
                           str+='<div class=\"layui-input-block\">';
                           str+='<textarea placeholder=\"\" id=\"'+item.name+'\" name=\"'+item.name+'\" class=\"layui-textarea\">'+item.value+'</textarea>';
                           str+='</div>';
                           str+='<div class=\"layui-form-mid layui-word-aux\" style=\"text-align: center;display: block;width: 100%;\">';
                           str+='<span class=\"x-red\">*</span>systemConfig(\"'+item.name+'\")获取  ';
                           if(item.tips != ''){
                               str+=item.tips;
                           }
                           str+='</div></div>';
                       }
                    });
                    str+='<div class=\"layui-form-item\">';
                    str+='<label for=\"L_repass\" class=\"layui-form-label\"></label>';
                    str+='<button class=\"layui-btn\" lay-filter=\"add\" lay-submit="">修改</button></div>';
                    $('#configForm').html(str);
                    form.render('select');

                }
            });

            //监听提交
            form.on('submit(add)',
                function(data) {
                    //发异步，把数据提交给php，提交修改
                    request.ajax(requestUri+'/admin/system/config/systemconfigedit', 'get', 'json', {
                        'token': adminToken,
                        'input':data.field
                    }, function (req) {
                        if(req.code == 1000){
                            layer.msg('修改成功');
                            setTimeout(function(){
                                location.reload();
                            },3000);
                        }
                    });
                    return false;
                });
        });
    //跳转配置list页面
    function configHref (e) {
        window.location.href=requesturi+'/admin/system.html?group='+e;
    }

    function foo(str){
        var temp = str.split(/[\n,]/g);
        for(var i =0;i<temp.length;i++){
            if(temp[i] == ""){
                temp.splice(i, 1);
                //删除数组索引位置应保持不变
                i--;
            }
        }
        return temp;
    }
</script>

</html>